package com.lee;

import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;


/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/11/29
 */
@SpringBootApplication
@Controller
public class ClientApplication {

	@Autowired
	private WebClient webClient;
	private String helloUri = "http://res.javaboy.org:8882/hello";
	public static void main(String[] args) {
		SpringApplication.run(ClientApplication.class, args);
	}

	private String retrieveMessages(String clientRegistrationId) {
		return webClient
				.get()
				.uri(helloUri)
				.attributes(clientRegistrationId(clientRegistrationId))
				.retrieve()
				.bodyToMono(String.class)
				.block();
	}

	@GetMapping(value = "/authorize", params = "grant_type=authorization_code")
	public String authorization_code_grant(Model model) {
		String msg = retrieveMessages("auth-code");
		model.addAttribute("msg", msg);
		return "index";
	}

	@GetMapping(value = "/authorize", params = "grant_type=client_credentials")
	public String client_credentials_grant(Model model) {
		String msg = retrieveMessages("client-creds");
		model.addAttribute("msg", msg);
		return "index";
	}

	@PostMapping(value = "/authorize", params = "grant_type=password")
	public String password_grant(Model model) {
		String msg = retrieveMessages("password");
		model.addAttribute("msg", msg);
		return "index";
	}

	@GetMapping("/")
	public String root() {
		return "redirect:/index";
	}

	@GetMapping("/index")
	public String index() {
		return "index";
	}

	@GetMapping("/token")
	@ResponseBody
	public String token(@RegisteredOAuth2AuthorizedClient("auth-code")
								OAuth2AuthorizedClient authorizedClient) {
		OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
		return accessToken.getTokenValue();
	}


}
